package com.lq.cart.controller;


import com.lq.cart.entity.Cart;
import com.lq.cart.service.ICartService;
import com.lq.cart.vo.CartDetailVO;
import com.lq.common.result.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 购物车表 前端控制器
 * </p>
 *
 * @author lq
 * @since 2025-08-23
 */
@RestController
@RequestMapping("/carts")
@Api(tags = "购物车管理", description = "购物车相关接口")
public class CartController {

    @Autowired
    private ICartService cartService;

    @ApiOperation(value = "添加商品到购物车", notes = "添加商品到购物车")
    @PostMapping("/")
    public Result addCart(@ApiParam(value = "商品id", required = true) @RequestParam Long productId) {
        boolean result = cartService.addProductToCart(productId);
        if (result) {
            return Result.success();
        } else {
            return Result.failed("添加购物车失败");
        }
    }

    @ApiOperation(value = "根据ID获取购物车项", notes = "根据购物车ID获取购物车项详细信息")
    @GetMapping("/{id}")
    public Result getCartById(@ApiParam(value = "购物车ID", required = true) @PathVariable Long id) {
        CartDetailVO cartDetailVO = cartService.getCartDetail(id);
        return Result.success(cartDetailVO);
    }

    @ApiOperation(value = "获取用户购物车列表", notes = "根据用户ID获取购物车列表")
    @GetMapping("/user/{userId}")
    public Result<List<CartDetailVO>> getCartByUserId(@ApiParam(value = "用户ID", required = true) @PathVariable Long userId) {
        List<CartDetailVO> cartList = cartService.getByUserId(userId);
        return Result.success(cartList);
    }

    @ApiOperation(value = "获取用户选中的购物车项", notes = "根据用户ID获取选中的购物车项")
    @GetMapping("/user/{userId}/checked")
    public Result getCheckedCartByUserId(@ApiParam(value = "用户ID", required = true) @PathVariable Long userId) {
        List<Cart> cartList = cartService.getCheckedItemsByUserId(userId);
        return Result.success(cartList);
    }

    @ApiOperation(value = "更新购物车项数量", notes = "根据购物车ID更新商品数量")
    @PutMapping("/{id}/quantity")
    public Result updateCartQuantity(@ApiParam(value = "购物车ID", required = true) @PathVariable Long id,
                                     @ApiParam(value = "商品数量", required = true) @RequestParam Integer quantity) {
        boolean result = cartService.updateCartQuantity(id, quantity);
        if (result) {
            return Result.success();
        } else {
            return Result.failed("更新购物车数量失败");
        }
    }

    @ApiOperation(value = "更新购物车项选中状态", notes = "根据购物车ID更新选中状态")
    @PutMapping("/{id}/checked")
    public Result updateCartChecked(@ApiParam(value = "购物车ID", required = true) @PathVariable Long id,
                                    @ApiParam(value = "选中状态(0-未选中,1-选中)", required = true) @RequestParam Integer checked) {
        boolean result = cartService.updateCartChecked(id, checked);
        if (result) {
            return Result.success();
        } else {
            return Result.failed("更新购物车选中状态失败");
        }
    }

    @ApiOperation(value = "更新购物车项", notes = "根据购物车ID更新购物车项信息")
    @PutMapping("/{id}")
    public Result updateCart(@ApiParam(value = "购物车ID", required = true) @PathVariable Long id,
                             @ApiParam(value = "购物车信息", required = true) @RequestBody Cart cart) {
        cart.setId(id);
        boolean result = cartService.updateById(cart);
        if (result) {
            return Result.success(cart);
        } else {
            return Result.failed("更新购物车失败");
        }
    }

    @ApiOperation(value = "删除购物车项", notes = "根据购物车ID删除购物车项")
    @DeleteMapping("/{id}")
    public Result deleteCart(@ApiParam(value = "购物车ID", required = true) @PathVariable Long id) {
        boolean result = cartService.removeCart(id);
        if (result) {
            return Result.success();
        } else {
            return Result.failed("删除购物车项失败");
        }
    }
}